package com.jd.org.service.impl;


import com.jd.basic.query.BaseQuery;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Department;
import com.jd.org.mapper.DepartmentMapper;
import com.jd.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
    }

    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Long queryCount(BaseQuery baseQuery) {
        return departmentMapper.queryCount(baseQuery);
    }

    @Override
    public List<Department> queryDepartment(BaseQuery baseQuery) {
        Long count = departmentMapper.queryCount(baseQuery);
        if (count == 0) {
            return new PageList<>();
        }
        List<Department> departmentList = departmentMapper.queryDepartment(baseQuery);
        return new PageList<>(count,departmentList);
    }

    @Override
    public void patchRemove(List<Long> ids) {
        departmentMapper.patchRemove(ids);
    }

    @Override
    public List<Department> departmentTreeQuery() {
        // 先把所有部门数据查询出来
        List<Department> departments = departmentMapper.loadAll();
        // 先创建一个map,部门所有数据放到里面，根据parent_id来寻找部门，就不用每次都去调数据库
        Map<Long,Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }

        /*
            定义数据哪来装整个部门结构，由于实体类中Department存在下级部门对象children，
            可以直接装顶级部门，再由顶级部门去接收下级部门
         */
        List<Department> departmentTree = new ArrayList<>();
        // 判断department是否存在partent_id;
        for (Department department : departments) {
            if (department.getParent_id() == null){
                departmentTree.add(department);
            } else {
                // 找到有上级部门的部门id
                Long parent_id = department.getParent_id();
                // 根据部门的上级部门id从map中找到上级部门的对象
                Department parentDepartment = map.get(parent_id);

                // 再将该部门放到上级部门中getChildren获取上级部门中的部门对象
                List<Department> children = parentDepartment.getChildren();
                children.add(department);

            }
        }
        return departmentTree;
    }
}
